package com.ququ.syb.payorder.controller;

import com.ququ.common.result.Result;
import com.ququ.syb.common.pojo.PayOrder;
import com.ququ.syb.payorder.service.PayOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class PayOrderController {
    @Autowired
    private PayOrderService payOrderService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");


    @RequestMapping("/query")
    public Result<PayOrder> getPayOrder(@RequestParam(required = false) String tradenum,
                                        @RequestParam(required = false) String orderno,
                                        @RequestParam(required = false) String appcode){
        PayOrder payOrder = null;
        if (StringUtils.isNotBlank(tradenum)) {
            payOrder = payOrderService.getPayOrder(tradenum);
        }else if (StringUtils.isNotBlank(orderno) && StringUtils.isNotBlank(appcode))
            payOrder = payOrderService.getPayOrder(orderno, appcode);
        if (payOrder == null)
            return new Result<>(-1,"无此订单");
        else
            return new Result<>(payOrder);
    }

    @RequestMapping("/list")
    public Result<List<PayOrder>> listPayOrder(@RequestParam(required = false) String beginDate,
                                               @RequestParam(required = false) String endDate,
                                               @RequestParam(defaultValue = "1") int currentpage,
                                               @RequestParam(defaultValue = "30") int pagesize,
                                               @RequestParam(required = false) Integer ordertype,
                                               @RequestParam(required = false) String paychannelid,
                                               @RequestParam(required = false) String accountid,
                                               @RequestParam(required = false) String appcode){
        Date begin;
        Date end;
        try {
            if (StringUtils.isBlank(beginDate)) {
                begin = new Date();
            } else
                begin = simpleDateFormat.parse(beginDate);
            begin.setHours(0);
            begin.setMinutes(0);
            begin.setSeconds(0);
            if (StringUtils.isBlank(endDate)) {
                end = new Date();
            } else
                end = simpleDateFormat.parse(endDate);
            end.setHours(23);
            end.setMinutes(59);
            end.setSeconds(59);
            List<PayOrder> payOrderList = payOrderService.listPayOrder(begin,end,currentpage,pagesize,ordertype,paychannelid,accountid,appcode);
            if (payOrderList == null || payOrderList.size()==0)
                return new Result<>(-1,"查无订单");
            else
                return new Result<>(payOrderList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<>(-1,"处理异常:"+e.getMessage());
        }
    }


    @RequestMapping("/save")
    public Result<PayOrder> save(@RequestBody PayOrder payOrder){
        PayOrder result = payOrderService.save(payOrder);
        if (result == null)
            return new Result<>(-1,"订单保存失败");
        else
            return new Result<>(result);
    }


    @RequestMapping("/update")
    public Result<PayOrder> update(@RequestBody PayOrder payOrder){
        PayOrder result = payOrderService.update(payOrder);
        if (result == null)
            return new Result<>(-1,"订单更新失败");
        else
            return new Result<>(result);
    }

}
